<template>
  <section>
    <div class="container">
      <div class="handle-box">
        <el-input
          v-model="real_name"
          placeholder="用户"
          clearable
          class="handle-input mr10"
          @keyup.enter.native="handleSearch"
        ></el-input>
        <el-input
          v-model="mobile"
          placeholder="手机号"
          clearable
          class="handle-input w200 mr10"
          @keyup.enter.native="handleSearch"
        ></el-input>
        <el-input
          v-model="card_number"
          placeholder="卡号"
          clearable
          class="handle-input w200 mr10"
          @keyup.enter.native="handleSearch"
        ></el-input>
        <el-select
          v-model="status"
          placeholder="筛选状态"
          clearable
          @keyup.enter.native="handleSearch"
          class="mr10"
        >
          <el-option label="全部" value=""></el-option>
          <el-option label="已制定" value="1"></el-option>
          <el-option label="进行中" value="2"></el-option>
          <el-option label="已完成" value="3"></el-option>
          <el-option label="已取消" value="4"></el-option>
          <el-option label="已失败" value="5"></el-option>
        </el-select>
        <el-button type="primary" icon="search" @click="handleSearch"
          >搜索</el-button
        >
        <el-button type="danger" @click="handleReset">清空</el-button>
      </div>
      <el-table
        :data="tableData"
        border
        class="table"
        ref="multipleTable"
        v-loading="loading"
      >
        <el-table-column prop="id" label="计划ID" width="80"></el-table-column>
        <el-table-column prop="real_name" label="姓名"></el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="amount" label="计划金额"></el-table-column>
        <el-table-column prop="card_number" label="银行卡号"></el-table-column>
        <el-table-column prop="bank_name" label="银行名称"></el-table-column>
        <el-table-column prop="channel_name" label="通道"></el-table-column>
        <el-table-column prop="create_time" label="创建时间"></el-table-column>
        <el-table-column prop="update_time" label="更新时间"></el-table-column>
        <el-table-column
          prop="unsettled_amount"
          label="未结算金额"
        ></el-table-column>
        <el-table-column
          prop="status"
          label="状态"
          :formatter="statusFormat"
        ></el-table-column>
      </el-table>
      <div class="pagination" style="text-align:center">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          @size-change="handleSizeChange"
          layout="sizes, prev, pager, next,total"
          :total="pagination.total"
          :current-page.sync="pagination.currentPage"
          :page-size="pagination.pageSize"
          :page-sizes="pagination.pageSizes"
          :hide-on-single-page="true"
        >
        </el-pagination>
      </div>
      <div class="dialog">
        <el-dialog
          :title="infoTitle"
          :visible.sync="dialogVisible"
          :before-close="handleClose"
          v-loading.lock="infoLoading"
        >
          <el-table :data="gridData" max-height="800">
            <el-table-column
              property="id"
              label="详情ID"
              width="80"
            ></el-table-column>
            <el-table-column
              property="request_no"
              label="交易号"
            ></el-table-column>
            <el-table-column
              property="amount"
              label="金额"
              width="80"
            ></el-table-column>
            <el-table-column
              property="type"
              label="类型"
              :formatter="infoTypeFormat"
              width="80"
            ></el-table-column>
            <el-table-column
              property="run_time"
              label="执行时间"
              width="150"
            ></el-table-column>
            <el-table-column
              property="charge"
              label="手续费"
              width="80"
            ></el-table-column>
            <el-table-column
              property="status"
              label="状态"
              :formatter="infoStatusFormat"
            ></el-table-column>
          </el-table>

          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="dialogVisible = false"
              >确 定</el-button
            >
          </span>
        </el-dialog>
      </div>
    </div>
  </section>
</template>

<script>
import { info } from '@/api/modules/plan'
import { planList } from '@/api/modules/branch'
import moment from 'moment'

export default {
  name: 'branchPlan',
  components: {},
  data() {
    return {
      loading: true,
      infoLoading: false,
      infoTitle: '',
      dialogVisible: false,
      tableData: [],
      real_name: '',
      mobile: '',
      card_number: '',
      status: '',
      pagination: {
        total: 0,
        pageSize: 20,
        currentPage: 1,
        pageSizes: [10, 20, 30]
      },
      gridData: []
    }
  },
  computed: {
    startTime: function() {
      console.log(this.timeRange)
      return this.timeRange ? this.timeRange[0] : null
    },
    endTime: function() {
      return this.timeRange ? this.timeRange[1] : null
    }
  },
  created() {
    this.getData()
  },
  methods: {
    search() {
      this.getData()
    },
    // 分页导航
    handleCurrentChange(val) {
      this.pagination.currentPage = val
      this.getData()
    },
    handleSizeChange(val) {
      this.pagination.pageSize = val
      this.getData()
    },
    // 获取数据
    getData() {
      this.loading = true
      planList({
        page: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
        mobile: this.mobile,
        real_name: this.real_name,
        card_number: this.card_number,
        status: this.status
      }).then(res => {
        if (res.status === 'success') {
          this.tableData = res.data.list
          this.pagination.total = res.data.pagination.total
        }
        this.loading = false
      })
    },
    dateFormatter(row, column) {
      return moment(row.create_time * 1000).format('YYYY-MM-DD')
    },
    statusFormat(row, column) {
      if (row.status === 0) {
        return <el-tag>未执行</el-tag>
      } else if (row.status === 1) {
        return <el-tag type="warning">已制定</el-tag>
      } else if (row.status === 2) {
        return <el-tag type="success">进行中</el-tag>
      } else if (row.status === 3) {
        return <el-tag type="primary">已完成</el-tag>
      } else if (row.status === 4) {
        return <el-tag type="info">已取消</el-tag>
      } else if (row.status === 5) {
        return <span style="color:#f00">{row.status_message}</span>
      }
    },

    infoStatusFormat(row, column) {
      if (row.status === 0) {
        return <el-tag>待执行</el-tag>
      } else if (row.status === 1) {
        return <el-tag type="warning">执行中</el-tag>
      } else if (row.status === 2) {
        return <el-tag type="success">成功</el-tag>
      } else if (row.status === 3) {
        return <span style="color:#f00">{row.fail_message}</span>
      }
    },

    infoTypeFormat(row, column) {
      if (row.type === 1) {
        return <span style="color:#F56C6C">消费</span>
      } else if (row.type === 2) {
        return <span style="color:#67C23A">还款</span>
      }
    },

    /**
     * 重置
     */
    handleReset() {
      this.page = 1
      this.pageSize = 20
      this.mobile = ''
      this.real_name = ''
      this.card_number = ''
      this.status = ''
      this.getData()
    },

    /**
     * 搜索
     */
    handleSearch() {
      // this.$router.replace({ query: '' })
      this.getData()
    },

    /**
     * handleClose
     */
    handleClose() {
      this.dialogVisible = false
    },

    /**
     * 获取计划详情
     */
    showInfo(row) {
      this.loading = true
      info({
        id: row.id
      }).then(res => {
        if (res.status === 'success') {
          this.gridData = res.data.detail
          ;(this.dialogVisible = true),
            (this.infoTitle =
              '用户' +
              row.real_name +
              '(' +
              row.mobile +
              ')卡片' +
              row.card_number +
              '在' +
              row.channel_name +
              '的计划详情')
        }
        this.loading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 100px;
  display: inline-block;
}
.w200 {
  width: 200px;
}

.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
</style>
